import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:
 * Date: 2025-03-19
 * Time: 10:58
 */
//如果我想要返回的是字符串如何进行修改呢？
class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> ret = new ArrayList<Integer>();

        Map<String, Integer> hash1 = new HashMap<String, Integer>();
        for(String str : words) hash1.put(str,hash1.getOrDefault(str, 0) + 1);

        int len = words[0].length(),  m = words.length;
        for(int i = 0; i < len; i++){
            Map<String, Integer> hash2 = new HashMap<String, Integer>();
            for(int left = i, right = i, count = 0; right + len < s.length(); right += len){
                String in = s.substring(right, right+len);
                hash2.put(in,hash2.getOrDefault(in, 0) +1);
                //getOrDefault的用法,get方法的用法，substring的用法
                if(hash2.get(in) <= hash1.getOrDefault(in,0)) count++;
                if(right - left + 1 > len * m){
                    String out = s.substring(left, left+len);

                    if(hash2.get(out) <= hash1.getOrDefault(out, 0)) count--;
                    hash2.put(out,hash2.get(out) - 1);
                    left += len;
                }
                if(count == m) ret.add(left);
            }
        }
        return ret;
    }
}

public class demo1 {
    public static void main(String[] args) {
        Solution solution = new Solution();
        String s = "wordgoodgoodgoodbestword";
        String[] words = {"word","good","best","good"};
        List<Integer> ret = solution.findSubstring(s,words);
        System.out.println(ret);
    }
}
